//
//  876. 链表的中间结点.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/9/5.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation
class Solution876 {
    // 链表的中间解点
    // [1,2,3,4,5]
    func middleNode(_ head: ListNode?) -> ListNode? {
        var slow = head
        var fast = head
        while fast != nil {
            if fast?.next == nil {
                break
            }
            slow = slow?.next
            fast = fast?.next?.next
        }
        return slow
    }
    static func test() {
        let solution = Solution876()
        let node1 = ListNode(1)
        let node2 = ListNode(2)
        let node3 = ListNode(3)
        let node4 = ListNode(4)
        let node5 = ListNode(5)
        
        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5
        
        let result = solution.middleNode(node1)
        print(result)
    }
}
